package com.key.modules.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.key.common.base.RespResult;
import com.key.modules.dto.CarrierAddDTO;
import com.key.modules.dto.CarrierEditDTO;
import com.key.modules.entity.Carrier;
import com.key.modules.entity.Driver;
import com.key.modules.entity.Vehicle;
import com.key.modules.mapper.CarrierMapper;
import com.key.modules.mapper.DriverMapper;
import com.key.modules.mapper.VehicleMapper;
import com.key.modules.service.CarrierService;
import com.key.modules.vo.CarrierDetailsVO;
import com.key.modules.vo.CarriersVO;
import com.key.modules.vo.DriverVO;
import com.key.modules.vo.VehicleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author wei
* @description 针对表【carrier(承运商表)】的数据库操作Service实现
* @createDate 2023-03-23 19:31:49
*/
@Service
@Slf4j
public class CarrierServiceImpl implements CarrierService{

    @Autowired
    CarrierMapper carrierMapper;

    @Autowired
    DriverMapper driverMapper;

    @Autowired
    VehicleMapper vehicleMapper;

    @Override
    public RespResult findCarrierList(Integer currentPage, Integer pageSize, String cName, String cType, String cRegisteredTime) {
        log.info("进入findCarrierList方法，查询承运商表（支持条件查询）"+
                "，currentPage = "+currentPage+
                "，pageSize = "+pageSize+
                "，cName = "+cName+
                "，cType = "+cType+
                "，cRegisteredTime = "+cRegisteredTime);

        Page<Carrier> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<Carrier> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(cName), Carrier::getCName, cName)
                .eq(StringUtils.isNotEmpty(cType), Carrier::getCType, cType)
                .eq(StringUtils.isNotEmpty(cRegisteredTime), Carrier::getCRegisteredTime, cRegisteredTime);

        Page<Carrier> carrierPage = carrierMapper.selectPage(page, lambdaQueryWrapper);

        List<Carrier> carrierList = carrierPage.getRecords();
        List<CarriersVO> carriersVOList = BeanUtil.copyToList(carrierList, CarriersVO.class);

        log.info("findCarrierList方法结束");
        return RespResult.success((int) carrierPage.getTotal(), carriersVOList);
    }

    @Override
    public CarrierDetailsVO findCarrierDetails(Integer cId) {
        log.info("进入findCarrierDetails方法，查询承运商详情"+
                "，cId = "+cId);

        LambdaQueryWrapper<Driver> driverLambdaQueryWrapper = new LambdaQueryWrapper<>();
        driverLambdaQueryWrapper.eq(Driver::getCId, cId);

        LambdaQueryWrapper<Vehicle> vehicleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        vehicleLambdaQueryWrapper.eq(Vehicle::getCId, cId);

        Carrier carrier = carrierMapper.selectById(cId);
        List<Driver> driverList = driverMapper.selectList(driverLambdaQueryWrapper);
        List<Vehicle> vehicleList = vehicleMapper.selectList(vehicleLambdaQueryWrapper);

        CarrierDetailsVO carrierDetailsVO = BeanUtil.copyProperties(carrier, CarrierDetailsVO.class);
        List<DriverVO> driverVOList = BeanUtil.copyToList(driverList, DriverVO.class, new CopyOptions());
        List<VehicleVO> vehicleVOList = BeanUtil.copyToList(vehicleList, VehicleVO.class, new CopyOptions());

        carrierDetailsVO.setDriverVOList(driverVOList);
        carrierDetailsVO.setVehicleVOList(vehicleVOList);

        log.info("findCarrierDetails方法结束");
        return carrierDetailsVO;
    }

    @Override
    public boolean addCarrier(CarrierAddDTO carrierAddDTO) {
        log.info("进入addCarrier方法，承运商新增"+
                "，carrierAddDTO = "+carrierAddDTO);

        Carrier carrier = BeanUtil.copyProperties(carrierAddDTO, Carrier.class);
        carrier.setCCreateTime(new Date());
        int result = carrierMapper.insert(carrier);

        log.info("addCarrier方法结束");
        return result > 0;
    }

    @Override
    public boolean removeCarrier(Integer cId) {
        log.info("进入removeCarrier方法，承运商删除"+
                "，cId = "+cId);

        int result = carrierMapper.deleteById(cId);

        log.info("removeCarrier方法结束");
        return result > 0;
    }

    @Override
    public boolean editCarrier(CarrierEditDTO carrierEditDTO) {
        log.info("进入editCarrier方法，承运商修改"+
                "，carrierEditDTO = "+carrierEditDTO);

        Carrier carrier = BeanUtil.copyProperties(carrierEditDTO, Carrier.class);
        int result = carrierMapper.updateById(carrier);

        log.info("editCarrier方法结束");
        return result > 0;
    }

}




